package com.gitee.mazhenggg.demo.auth.handle;

import com.gitee.mazhenggg.demo.auth.security.SecurityUserDetails;
import com.gitee.mazhenggg.demo.auth.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author mazhenggg
 */
@Component
@Slf4j
public class AuthLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws ServletException, IOException {
        log.debug("login success");
        List<String> heads = (List<String>) response.getHeaderNames();
        SecurityUserDetails securityUserDetails = (SecurityUserDetails) auth.getPrincipal();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        try (PrintWriter out = response.getWriter()){
            out.write("success");
        } catch (Exception e) {
            log.error("json error: {}", e);
        }

    }


}
